﻿using BarrageGameAssistant.Core.Configs;
using ImageProcess;

namespace BarrageGameAssistant.UIForms
{
    public partial class FrmGameSetting : Form
    {
        public Settings? Settings { get; set; }

        /// <summary>
        /// 游戏配置
        /// </summary>
        public GameProject? CurrentGame { get; set; }

        public FrmGameSetting()
        {
            InitializeComponent();
        }

        private void FrmGameSetting_Load(object sender, EventArgs e)
        {
            cbbCommonResolutions.SelectedIndex = 0;

            if (CurrentGame != null)
            {
                this.chkAutoGameTop.Checked = CurrentGame.AutoGameTop;
                this.chkAutoGameForeground.Checked = CurrentGame.AutoGameForeground;
                this.chkAutoGameFocus.Checked = CurrentGame.AutoGameFocus;
                this.chkAutoGameWindowNormal.Checked = CurrentGame.AutoGameWindowNormal;
                this.chkAutoChangeGameLocation.Checked = CurrentGame.AutoChangeGameLocation;
                this.chkAutoChangeGameSize.Checked = CurrentGame.AutoChangeGameSize;
                this.chkAutoThumbnail.Checked = CurrentGame.AutoThumbnail;

                this.nudWindowLocationX.Value = CurrentGame.WindowsSetLocation.X;
                this.nudWindowLocationY.Value = CurrentGame.WindowsSetLocation.Y;
                this.nudWindowSetFixWidth.Value = CurrentGame.WindowSetFixWidth;
                this.nudWindowSetFixHeigh.Value = CurrentGame.WindowSetFixHeigh;
                this.nudWindowSetOffsetX.Value = CurrentGame.WindowSetOffsetX;
                this.nudWindowSetOffsetY.Value = CurrentGame.WindowSetOffsetY;

                this.cbbIsCropImage.SelectedIndex = CurrentGame.IsCropImage ? 1 : 0;
                this.nudCropImageSizeWidth.Value = CurrentGame.CropImageSize.Width;
                this.nudCropImageSizeHeight.Value = CurrentGame.CropImageSize.Height;
                this.nudCropImageLocationX.Value = CurrentGame.CropImageLocation.X;
                this.nudCropImageLocationY.Value = CurrentGame.CropImageLocation.Y;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //var size = this.cbbCommonResolutions.Text.ConvertSize();
            //if (size.Width > Settings?.DefaultResolutionRatioSize.Width || size.Height > Settings?.DefaultResolutionRatioSize.Height)
            //{
            //    MessageBox.Show(this, $"裁剪分辨率不能大于场景分辨率.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

            if (CurrentGame != null)
            {
                CurrentGame.AutoGameTop = this.chkAutoGameTop.Checked;
                CurrentGame.AutoGameForeground = this.chkAutoGameForeground.Checked;
                CurrentGame.AutoGameFocus = this.chkAutoGameFocus.Checked;
                CurrentGame.AutoGameWindowNormal = this.chkAutoGameWindowNormal.Checked;
                CurrentGame.AutoChangeGameLocation = this.chkAutoChangeGameLocation.Checked;
                CurrentGame.AutoChangeGameSize = this.chkAutoChangeGameSize.Checked;
                CurrentGame.AutoThumbnail = this.chkAutoThumbnail.Checked;

                CurrentGame.WindowsSetLocation = new Point(Convert.ToInt32(this.nudWindowLocationX.Value), Convert.ToInt32(this.nudWindowLocationY.Value));
                CurrentGame.WindowSetFixWidth = Convert.ToInt32(this.nudWindowSetFixWidth.Value);
                CurrentGame.WindowSetFixHeigh = Convert.ToInt32(this.nudWindowSetFixHeigh.Value);
                CurrentGame.WindowSetOffsetX = Convert.ToInt32(this.nudWindowSetOffsetX.Value);
                CurrentGame.WindowSetOffsetY = Convert.ToInt32(this.nudWindowSetOffsetY.Value);

                CurrentGame.IsCropImage = cbbIsCropImage.SelectedIndex == 1 ? true : false;
                CurrentGame.CropImageSize = new Size(Convert.ToInt32(this.nudCropImageSizeWidth.Value), Convert.ToInt32(this.nudCropImageSizeHeight.Value));
                CurrentGame.CropImageLocation = new Point(Convert.ToInt32(this.nudCropImageLocationX.Value), Convert.ToInt32(this.nudCropImageLocationY.Value));
            }

            this.DialogResult = DialogResult.OK;

        }

        private void cbbCommonResolutions_SelectedIndexChanged(object sender, EventArgs e)
        {
            var size = this.cbbCommonResolutions.Text.ConvertSize();
            this.nudCropImageSizeWidth.Value = size.Width;
            this.nudCropImageSizeHeight.Value = size.Height;
        }
    }
}
